define(['app'], function(app) {
    'use strict';

    var controller = function controller($scope, Productmanager_publish, GoPage, Auth, GoCatTypeAuth, GoPicture, $stateParams, GoDialog, $ionicModal,$timeout) {
        //页面跳转
        $scope.onshow_productmanager_publish_more = function(goods_id) {
            GoPage.go_states("productmanager_publish_more", {
                "goods_id": goods_id
            });
        }
        $scope.onshow_transfer_publish = function() {
            GoPage.go_states("transfer_publish");
        }
        $scope.onshow_searchpro_common = function() {
            GoPage.go_states("searchpro_common");
        }

        //类型
        $scope.productType = 'new'; //默认是"全新"
        $scope.changProductType = function(type) {
            $scope.productType = type;
        }

        //收起/展开--商品规格
        $scope.formatToggle = function() {
                $scope.isShowFormat = !$scope.isShowFormat;
            }
            //收起/展开--配件
        $scope.formatToggle2 = function() {
            $scope.isShowFormat2 = !$scope.isShowFormat2;
        }

        //数据模型
        $scope.productModel = new Object();
        $scope.productModel.goods_id = $stateParams.goods_id;
        //$scope.productModel.brand_id ='0';
        $scope.productModel.cat_id = ''; //分类string
        $scope.productModel.property = new Array(); //商品属性数组
        $scope.proArray = new Array(); //通过其获取页面的属性值，没其他作用
        $scope.productModel.dlytype_select = {id:0,name:"请选择运费模板"};
        $scope.productModel.cat_list_select = {id:0,name:"请选择自定义分类"};
        $scope.productModel.specification = "0";
        $scope.productModel.name = '';
        $scope.productModel.cat_id = '';
        $scope.productModel.bn = '';
        $scope.productModel.keywords = '';
        $scope.productModel.brief = '';
        $scope.productModel.image = '';

        $scope.productModel.dlytype_id = '0';
        $scope.productModel.is_freepost = false;
        $scope.productModel.is_marketable = true;
        $scope.productModel.is_salewithoutstore = false;
        $scope.productModel.cat_list_id = '0'; //自定义分类
        $scope.productModel.meta_keyword = '';
        $scope.productModel.meta_describe = '';
        $scope.productModel.related_goods = '';
        $scope.productModel.suitgoods_id = '';

        if($scope.input == undefined) $scope.input = new Object();
        $scope.input.uploads = new Array(); //图片上传数组

        /*******************分类三级联动*******************/
        $scope.input.store_id = Auth.get_store_id(); //隐藏的企业id的绑定数据
        $scope.input.brand = null; //这是品牌下拉框的绑定数据
        $scope.input.cat1 = null;
        $scope.input.cat2 = null;
        $scope.input.cat3 = null;
        //下拉侦听
        $scope.listen_brand = function(data, obj) {
            $scope.input.cat1 = null;
            $scope.input.cat2 = null;
            $scope.input.cat3 = null;
        };
        $scope.listen_cat1 = function(data, obj) {
            $scope.input.cat2 = null;
            $scope.input.cat3 = null;
        };
        $scope.listen_cat2 = function(data, obj) {
            $scope.input.cat3 = null;
        };
        //定义绑定
        var items_cat = new Array();
        var obj_brand = {
            name: 'input.brand'
        };
        var obj1_cat = {
            name: 'input.cat1'
        };
        var obj2_cat = {
            name: 'input.cat2'
        };
        var obj3_cat = {
            name: 'input.cat3'
        };
        items_cat.push(obj_brand);
        items_cat.push(obj1_cat);
        items_cat.push(obj2_cat);
        items_cat.push(obj3_cat);
        GoCatTypeAuth.init_load($scope, items_cat);
        /****************分类三级联动************************/
		$scope.doSelectbrand = function(){
			//id是值 name是显示
			GoDialog.showSelect("品牌",$scope.brands,$scope.input.brand,function(data){
				$scope.input.brand = data;
				$scope.productModel.brand_id = data.id;
			});
		}
		$scope.doSelectCat1 = function(){
			//id是值 name是显示
			GoDialog.showSelect("大分类",$scope.cat1_items,$scope.input.cat1,function(data){
                $scope.input.cat1 = data;
                $scope.productModel.cat_id = data.id;
			});
		}
		$scope.doSelectCat2 = function(){
			//id是值 name是显示
			GoDialog.showSelect("中分类",$scope.cat2_items,$scope.input.cat2,function(data){
                $scope.input.cat2 = data;
                $scope.productModel.cat_id = data.id;
			});
		}
		$scope.doSelectCat3 = function(){
			//id是值 name是显示
			GoDialog.showSelect("小分类",$scope.cat3_items,$scope.input.cat3,function(data){
                $scope.input.cat3 = data;
                $scope.productModel.cat_id = data.id;
			});
		}
		$scope.doSelectdlytype = function(){
			//id是值 name是显示
			GoDialog.showSelect("运费模板",$scope.dlytypes,$scope.productModel.dlytype_select,function(data){
				$scope.productModel.dlytype_id = data.id;
				$scope.productModel.dlytype_select = data;
			});
		}
		$scope.doSelectCustomType = function(){
            //id是值 name是显示
            var list = new Array();
            $scope.catlist.forEach(obj => {
                if(obj.id == undefined)
                obj.id = obj.custom_cat_id;
                if(obj.name == undefined)
                obj.name = obj.cat_name;
                list.push(obj);
            });
			GoDialog.showSelect("大分类",list,$scope.productModel.cat_list_select,function(data){
				$scope.productModel.cat_list_id = data.id;
				$scope.productModel.cat_list_select = data;
			});
		}

        //提交数据
        $scope.edit = function() {
            for(var i = 0; i < $scope.propertys.length; i++) {
                if($scope.proArray[i]) { //如果填了属性值，则要获取该属性的id
                    $scope.pro = new Object(); //每次都要把该对象push到$scope.productModel.property里面
                    $scope.pro.id = document.getElementById("radio[" + i + "]").value; //属性id
                    $scope.pro.value = $scope.proArray[i]; //属性值
                    $scope.productModel.property.push($scope.pro);
                }
            }

            if(!$scope.input.brand) {
                GoDialog.toast("请选择品牌！");
            } else if(!$scope.input.cat3) {
                GoDialog.toast("请选择分类！");
            }
            //$scope.productModel.cat_id = $scope.input.cat1.name+"/"+$scope.input.cat2.name+"/"+$scope.input.cat3.name+":"+$scope.input.cat3.id;
            //console.log($scope.productModel.cat_id)
            $scope.productModel.image = $scope.input.uploads;
           // console.log($scope.productModel.image)
            Productmanager_publish.productmanager_publish_edit($scope.productModel.goods_id, $scope.productType, $scope.productModel.cat_id, $scope.productModel.name, $scope.productModel.property, $scope.productModel.bn, $scope.productModel.keywords, $scope.productModel.brief, $scope.productModel.image, $scope.productModel.brand_id, $scope.productModel.specification, $scope.productModel.dlytype_id, $scope.productModel.is_freepost, $scope.productModel.is_marketable, $scope.productModel.is_salewithoutstore, $scope.productModel.cat_list_id, $scope.productModel.related_goods, $scope.productModel.meta_keyword, $scope.productModel.meta_describe, $scope.productModel.suitgoods_id, function(data) {
                if(data.success) {
                    GoDialog.toast("提交数据成功！");
                } else {
                    GoDialog.toast("提交数据失败！");
                    $scope.productModel.property = new Array(); //提交失败时，要初始化该数组，避免里面的数据重复
                }
            });
        }

        //数据初始化
        $scope.init_data = function() {
            Productmanager_publish.productmanager_publish_brand(function(data) {
                $scope.brands = data.brand;
            });
            Productmanager_publish.productmanager_publish_fare(function(data) {
                $scope.dlytypes = data.dlytype;
            });
            Productmanager_publish.productmanager_publish_property('custom_cat_id', function(data) {
                $scope.propertys = data.property;
            });
            Productmanager_publish.specification('custom_cat_id', function(data) {
                $scope.specifications = data.specification;
                $scope.guiGe2 = new Array($scope.specifications.length); //作为多维数组
                for(var i = 0; i < $scope.specifications.length; i++) {
                    $scope.guiGe2[i] = $scope.specifications[i].value.slice(0); //必须用深拷贝
                }
                console.log($scope.guiGe2)
                $scope.guiGe3 = $scope.guiGe2.slice(0); //复制一份
                console.log($scope.guiGe3)
            });
            Productmanager_publish.get_cat_list(function(data) {
                $scope.catlist = data.custom_cat;
            });
        }
        $scope.init_data(); //必须在定义之后

        //字数统计
        $scope.$watch('productModel.brief', function(newValue, oldValue) {
            $scope.textLength = newValue.length;
            if($scope.textLength > 70) {
                $scope.productModel.brief = newValue.substring(0, 70); //超过70，则取前面的70个字符
            }
        })

        /*==========图片上传、删除=============*/
        $scope.doShowFile = function() {
                document.getElementById("upfilebutton").click();
            }
        //图片上传
        $scope.addStoreImg = function() {
            GoPicture.upload(function(res) {
                if(!res.success) {
                    GoDialog.toast(res.msg);
                    return;
                }
                console.log(res)
                $scope.photo = new Object();
                $scope.photo.url = res.result.image_src; //图片 显示用
                $scope.photo.id = res.result.image_id; //图片id显示用
                $scope.input.uploads.push($scope.photo);
            });
        }
            //图片移除
        $scope.ondelete = function(index) {
            $scope.input.uploads.splice(index, 1);
        };
        //图片放大删除
        $scope.openFileActSheet = function(index) {
            var buttons = new Array();
            buttons.push({ text: '删除图片', style: '' });
            buttons.push({ text: '显示大图', style: '' });

            GoDialog.sheet('图片操作', '取消', buttons, function(bindex) {

                if(bindex == 0) {
                    GoDialog.confirm('删除图片', '是否要删除该图片？', function() {
                        $timeout(function() {
                            $scope.input.uploads.splice(index, 1);
                        }, 1);
                    })
                } else if(bindex == 1) {
                }
                return true;
            });
        }

        /*==========图片上传、删除=============*/

        //全选--规格
        $scope.checkAll_guige = function($event) {
            var childs = $event.target.parentNode.parentNode.children; //前3个是不需要的
            for(var i = 3; i < childs.length; i++) {
                if(childs[i].children[0].checked) {
                    childs[i].children[0].checked = "";
                } else {
                    childs[i].children[0].checked = true;
                }
            }
            $scope.getGuiGeValue();
        }

        //获取规格的值
        $scope.getGuiGeValue = function() {
            $scope.guiGe = new Array(); //存放所有规格的id和name
            var ggBoxs = document.getElementsByClassName("ggBoxs");
            var valArray = new Array(); //改方法内的临时数组,[0]存放id，[1]存放name
            for(var i = 0; i < ggBoxs.length; i++) {
                if(ggBoxs[i].checked) {
                    $scope.guiGeObj = new Object(); //把该对象push到$scop.guiGe里
                    valArray = ggBoxs[i].value.split(",");
                    $scope.guiGeObj.id = valArray[0];
                    $scope.guiGeObj.name = valArray[1];
                    $scope.guiGe.push($scope.guiGeObj); //push
                }
            }
            //console.log($scope.guiGe.splice(0,2))
            //console.log($scope.guiGe)
        }

        $scope.a111 = function(parentIndex, index) {
            console.log(parentIndex + "子" + index)
            $scope.guiGe3[parentIndex].splice(0, $scope.guiGe3[parentIndex].length); //先清空
            $scope.guiGe3[parentIndex].push($scope.guiGe2[parentIndex][index]);
            console.log($scope.guiGe3)
        }

        //4-20新增--模态对话框==添加配件
        $ionicModal.fromTemplateUrl("templates/modal/searchgoods_common.html", {
            scope: $scope,
            animation: "slide-in-up"
        }).then(function(modal) {
            $scope.modal = modal;
        });
        //打开
        $scope.openModal = function() {
                $scope.modal.show();
            }
            //隐藏
        $scope.closeModal = function() {
            $scope.modal.hide();
        }

        //5-8新增--模态对话框==制造商检索页面---为了看效果，临时加在这个页面里
        $ionicModal.fromTemplateUrl("templates/modal/search_manufactor.html", {
            scope: $scope,
            animation: "slide-in-up"
        }).then(function(modal) {
            $scope.modal = modal;
        });
        //打开
        $scope.openModal_manufactor = function() {
                $scope.modal.show();
            }
            //隐藏
        $scope.closeModal_manufactor = function() {
            $scope.modal.hide();
        }

    }

    controller.$inject = ['$scope', 'Productmanager_publish', 'GoPage', 'Auth', 'GoCatTypeAuth', 'GoPicture', '$stateParams', 'GoDialog', '$ionicModal','$timeout'];

    return app.registerController("productmanager_publishctrl",controller);
});